package com.example.administrator.newjiajiesong.network.http;

import com.example.administrator.newjiajiesong.BaseApplication;
import com.example.administrator.newjiajiesong.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by: xudiwei
 * <p>
 * on: 2017/4/24.
 * <p>
 * 描述：生命树http请求数据转换类
 */

public class LifeTreeConverterFactory extends Converter.Factory {
    private static final String TAG = "LifeTreeConverterFactor";
    private static final int STATE_SUCCESS = 1;
    private static final int STATE_FAILED = 0;
    private Gson mGson = new Gson();

    public static LifeTreeConverterFactory create() {
        return new LifeTreeConverterFactory();
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new LifeTreeResponseConverter<>(type);
    }

    /**
     * 响应解析
     */
    private class LifeTreeResponseConverter<T> implements Converter<ResponseBody, T> {
        private Type mType;

        LifeTreeResponseConverter(Type type) {
            mType = type;
        }

        @Override
        public T convert(ResponseBody value) throws IOException {
            return parser2(value);
        }

        /**
         * 第一种解析
         *test测试用的，用于打印响应数据json
         * @param value
         * @return
         * @throws IOException
         */
        private T parser(ResponseBody value) throws IOException {
            String response;
            try {
                response = value.string();
                if (mType == String.class) {
                    return (T) response;
                } else {
                    JSONObject jsonObject = new JSONObject(response);
                    int state = jsonObject.getInt("status");
                    if (state == STATE_FAILED) {
                        String msg = jsonObject.getString("msg");
                        throw new LifeTreeAPIException(state, msg);
                    } else {
                        String data = jsonObject.getString("data");
                        return mGson.fromJson(data, mType);
                    }
                }
            } catch (JSONException e) {
                throw new LifeTreeAPIException(0, BaseApplication.getContext().getString(R.string.text_http_data_parser_error));
            } finally {
                value.close();
            }
        }

        /**
         * 第二种解析
         *
         * @param value
         * @return
         */
        private T parser2(ResponseBody value) throws IOException {
            String response;
            try {
                response = value.string();
                JSONObject jsonObject = new JSONObject(response);
                int state = jsonObject.getInt("status");
                if (state == STATE_FAILED) {
                    String msg = jsonObject.getString("msg");
                    throw new LifeTreeAPIException(state, msg);
                } else {
                    String data = jsonObject.getString("data");
                    if (mType == String.class) {
                        return (T) data;
                    } else {
                        return mGson.fromJson(data, mType);
                    }
                }
            } catch (JSONException e) {
                throw new LifeTreeAPIException(0, BaseApplication.getContext().getString(R.string.text_http_data_parser_error));
            } finally {
                value.close();
            }
        }
    }


}
